﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using PNunitToolsClass;

namespace PNunitAgentStarter
{
    public partial class Form1 : Form
    {
        private AgentConfig agentConfig;

        public Form1()
        {
            InitializeComponent();
        }

        private void button_agent_load_Click(object sender, EventArgs e)
        {
            string d = Environment.CurrentDirectory;
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "conf files (*.conf)|*.conf";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    agentConfig = AgentConfig.Load(openFileDialog.FileName);
                    MessageBox.Show("Load succeed!");
                    numericUpDown_agent_port.Value = agentConfig.Port;
                    textBox_agent_pathToAssemblies.Text = agentConfig.PathToAssemblies;
                    numericUpDown_agent_port.Enabled =
                        textBox_agent_pathToAssemblies.Enabled =
                        button_agent_select.Enabled =
                        button_agent_save.Enabled =
                        button_start.Enabled = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("Load failed! {0}", ex.Message));
                }
            }
            Environment.CurrentDirectory = d;
        }

        private void button_agent_select_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderbrowerdialog = new FolderBrowserDialog();
            if (folderbrowerdialog.ShowDialog() == DialogResult.OK)
            {
                textBox_agent_pathToAssemblies.Text = folderbrowerdialog.SelectedPath;
                if (textBox_agent_pathToAssemblies.Text == "")
                {
                    textBox_agent_pathToAssemblies.Text = ".";
                }
            }
        }

        private void button_agent_new_Click(object sender, EventArgs e)
        {
            this.agentConfig = new AgentConfig(10000);
            numericUpDown_agent_port.Value = agentConfig.Port;
            textBox_agent_pathToAssemblies.Text = agentConfig.PathToAssemblies;
            numericUpDown_agent_port.Enabled =
                textBox_agent_pathToAssemblies.Enabled =
                button_agent_select.Enabled =
                button_agent_save.Enabled =
           button_start.Enabled = true;
        }

        private void button_agent_save_Click(object sender, EventArgs e)
        {
            agentConfig.Port = Convert.ToInt32(numericUpDown_agent_port.Value);
            agentConfig.PathToAssemblies = textBox_agent_pathToAssemblies.Text;
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "conf files (*.conf)|*.conf";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    agentConfig.Save(saveFileDialog.FileName);
                    MessageBox.Show("Save succeed!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("Save failed! {0}", ex.Message));
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {            
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            p.StandardInput.WriteLine(string.Format("start pnunit-agent.exe {0} {1}",
                Convert.ToInt32(numericUpDown_agent_port.Value),
                textBox_agent_pathToAssemblies.Text));
        }
    }
}
